/*
 * Copyright (C) 2011 Cyril Mottier (http://www.cyrilmottier.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package es.android.alertes.objects;

import greendroid.graphics.drawable.DrawableStateSet;
import greendroid.graphics.drawable.MapPinDrawable;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MapPointer {

	private Context ctx;
	
    private static Random sRandom = new Random();

    private static final int[] PRESSED_STATE = {
        android.R.attr.state_pressed
    };

    public BasicItemizedOverlay getItimizedOverlays(Context ctx, OverlayItem[] zonas) {
    	this.ctx = ctx;
        final Resources r = ctx.getResources();
        
            ColorStateList pinCsl = createRandomColorStateList();
            ColorStateList dotCsl = createRandomColorStateList();
            BasicItemizedOverlay itemizedOverlay = new BasicItemizedOverlay(new MapPinDrawable(r, pinCsl, dotCsl));

            for (int j = 0; j < zonas.length; j++) {
                itemizedOverlay.addOverlay(zonas[j]);
            }

        return itemizedOverlay;
    }
    
    public GeoPoint getCenterPoint(OverlayItem[] zonas)
    {
    	int la = 0;
    	int lo = 0;
    	
    	for (int i = 0; i < zonas.length; i++)
    	{
    		la += zonas[i].getPoint().getLatitudeE6();
    		lo += zonas[i].getPoint().getLongitudeE6();
    	}
    	
    	return new GeoPoint(la/zonas.length, lo/zonas.length);
    }

    private class BasicItemizedOverlay extends ItemizedOverlay<OverlayItem> {

        private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

        public BasicItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
        }

        public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
        }

        @Override
        protected OverlayItem createItem(int i) {
            return mOverlays.get(i);
        }

        @Override
        public int size() {
            return mOverlays.size();
        }

        @Override
        protected boolean onTap(int index) {
        	Dialogos dialogs = new Dialogos(ctx);
        	
        	dialogs.mostrarDialogo(mOverlays.get(index).getTitle(), "Text");
            return true;
        }

    }

    private ColorStateList createRandomColorStateList() {

        int[][] states = new int[2][];
        int[] colors = new int[2];

        final int randomColor = randomColor();

        states[0] = PRESSED_STATE;
        colors[0] = addRGB(randomColor, -50);

        states[1] = DrawableStateSet.EMPTY_STATE_SET;
        colors[1] = randomColor;

        return new ColorStateList(states, colors);
    }

    private static final int randomColor() {
        int r = sRandom.nextInt(256);
        int g = sRandom.nextInt(256);
        int b = sRandom.nextInt(256);
        return Color.rgb(r, g, b);
    }

    private static int addRGB(int color, int amount) {
        int r = constrain(Color.red(color) + amount, 0, 255);
        int g = constrain(Color.green(color) + amount, 0, 255);
        int b = constrain(Color.blue(color) + amount, 0, 255);
        return Color.rgb(r, g, b);
    }

    public static int constrain(int amount, int low, int high) {
        return amount < low ? low : (amount > high ? high : amount);
    }

}
